/*
 * @Author: jiangxin
 * @Date: 2022-11-06 16:23:28
 * @Company: orientsec.com.cn
 * @Description:
 */

const commonconfig = require("./webpack.common");
const devconfig = require("./webpack.dev");
const prodconfig = require("./webpack.prod");

const { mergeWithCustomize } = require("webpack-merge");

const currentScript = process.env.npm_lifecycle_event;

// filename:业务名称
// env:开发[dev]/生产[prod]
// action:动作 serve/build
const {
  groups: { filename, env, action = "serve" }
} = currentScript?.match?.(
  /(?<filename>\w+-{1}\w+)-{1}(?<env>\w+)(:{1}(?<action>\w+))?/i
);

const _merge = mergeWithCustomize({
  customizeArray(a, b, key) {
    if (key === "plugins") {
      return b;
    }
    // Fall back to default merging
    return undefined;
  }
});

module.exports = (...arg) => {
  // 开发环境
  if (env === "dev") {
    return _merge(commonconfig(), devconfig(...arg));
  }

  // 生产环境
  if (env === "prod") {
    return _merge(commonconfig(), prodconfig(...arg));
  }
};

